{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装 Apache TVM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 步骤1: 检查系统环境\n",
    "\n",
    "确认当前目录为`~/workspace`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外，我们需要确认nvcc的版本需要和nvidia-smi中CUDA的驱动版本一致。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!nvcc --version\n",
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!sudo rm /usr/local/cuda\n",
    "!sudo ln -s /usr/local/cuda-10.2 /usr/local/cuda\n",
    "!nvcc --version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 步骤2: 安装编译工具\n",
    "\n",
    "这里，我们通过操作系统的安装包管理器，安装编译TVM所需的工具\n",
    "\n",
    " * TVM需要支持C++14的C++编译器。这里我们用一下命令安装g++-7.2\n",
    " * CMake（3.0以上版本）\n",
    " * LLVM (4.0以上版本)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##### Uncomment the following lines if you are trying to install TVM in SageMaker Studio ####\n",
    "# !apt update\n",
    "# !apt install -y libcudnn7-dev libnvinfer-dev cuda-nvcc-10-2 cuda-nvrtc-10-2 cuda-nvrtc-dev-10-2 \n",
    "# !apt install -y cmake llvm-6.0-dev\n",
    "\n",
    "#### Uncomment the following lines if you are trying to install TVM in SageMaker Notebook ####\n",
    "!sudo yum update -y\n",
    "!sudo yum install -y gcc72 gcc72-c++ cmake3 llvm6.0-devel\n",
    "!sudo rm /etc/alternatives/gcc\n",
    "!sudo ln -s /usr/bin/gcc72 /etc/alternatives/gcc\n",
    "!gcc --version\n",
    "!g++ --version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 步骤3: 下载最新版本的Apache TVM\n",
    "\n",
    "Apache TVM 的官方Github地址是 http://github.com/apache/incubator-tvm ，为了提升下载速度可以从 https://gitee.com/mirrors/tvm 下载 Apache TVM."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone --recursive https://gitee.com/mirrors/tvm.git tvm_upstream    \n",
    "!cd tvm_upstream && git fetch origin master && git merge origin/master && cd .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !mkdir -p tvm_upstream/build\n",
    "# !cp config.cmake tvm_upstream/build\n",
    "# !cd tvm_upstream/build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cd tvm_upstream && make cython3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ --user decorator numpy scipy attrs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, \"tvm_upstream/python\")\n",
    "sys.path.insert(0, \"tvm_upstream/topi/python\")\n",
    "\n",
    "import tvm\n",
    "import topi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!PYTHONPATH=`pwd`/tvm_upstream/python:`pwd`/tvm_upstream/topi/python python3 tvm_upstream/tutorials/optimize/opt_conv_cuda.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir -p ~/.tvm/tophub\n",
    "!cp cuda_v0.08.log ~/.tvm/tophub\n",
    "!cp llvm_v0.04.log ~/.tvm/tophub\n",
    "\n",
    "!mkdir -p ~/.tvm_test_data/data\n",
    "!cp cat.png ~/.tvm_test_data/data\n",
    "!cp imagenet1000_clsid_to_human.txt ~/.tvm_test_data/data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!aws s3 cp s3://sagemaker-cn-north-1-338598867091/resnet18_v1-a0666292.zip ~/.mxnet/models/\n",
    "!cd ~/.mxnet/models/ && unzip resnet18_v1-a0666292.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!PYTHONPATH=`pwd`/tvm_upstream/python:`pwd`/tvm_upstream/topi/python python3 tvm_upstream/tutorials/frontend/from_mxnet.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "instance_type": "ml.g4dn.xlarge",
  "kernelspec": {
   "display_name": "conda_mxnet_p36",
   "language": "python",
   "name": "conda_mxnet_p36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
